package com.hq.fxh.No3_TuiGuang.MyMoBan.Activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 小谢
 * @ClassName HomePagerAdapter
 * @Description :
 * @Created 2016/11/8 0008
 */
public class HomePagerAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> viewList;
    private List<String> titleList;
    private ViewPager viewPager;
    private int currenttab = -1;
    FragmentManager fm;

    public void setViewList(List<Fragment> viewList) {
        this.viewList = viewList;
    }

    public void setTitleList(List<String> titleList) {
        this.titleList = titleList;
    }

    public HomePagerAdapter(FragmentManager fm, List<Fragment> viewList, List<String> titleList, ViewPager viewPager) {
        super(fm);
        this.fm = fm;
        this.viewList = viewList;
        this.titleList = titleList;
        this.viewPager = viewPager;
    }


    @Override
    public Fragment getItem(int position) {
        return viewList.get(position);
    }

    @Override
    public int getCount() {
        return viewList.size();
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        super.finishUpdate(container);
        //获取当前的视图是位于ViewGroup的第几个位置，用来更新对应的覆盖层所在的位置
        int currentItem=viewPager.getCurrentItem();
        if (currentItem==currenttab)
        {
            return ;
        }
        currenttab=viewPager.getCurrentItem();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
    public int getItemPosition(Object object){
        return POSITION_NONE;
    }

    public void setFragments(ArrayList fragments) {
        if(this.viewList != null){
            FragmentTransaction ft = fm.beginTransaction();
            for(Fragment f:this.viewList){
                ft.remove(f);
            }
            ft.commit();
            ft=null;
            fm.executePendingTransactions();
        }
        this.viewList = fragments;
        notifyDataSetChanged();
    }
}
